package com.jeeplus.test.website.infoPublish.service.Impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeeplus.sys.service.UserService;
import com.jeeplus.test.tools.resp.Convert2Bean;
import com.jeeplus.test.website.infoPublish.domain.entity.InfoPublish;
import com.jeeplus.test.website.infoPublish.domain.vo.InfoPublishReqVo;
import com.jeeplus.test.website.infoPublish.domain.vo.InfoPublishRespVo;
import com.jeeplus.test.website.infoPublish.mapper.InfoPublishMapper;
import com.jeeplus.test.website.infoPublish.service.InfoPublishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class InfoPublishServiceImpl extends ServiceImpl<InfoPublishMapper, InfoPublish> implements InfoPublishService {

    @Autowired
    private InfoPublishMapper infoPublishMapper;
    @Autowired
    private UserService userService;

    @Override
    public Page<InfoPublishRespVo> getInfoPublishPage(InfoPublishReqVo infoPublishReqVo, long current, long size) {

        InfoPublish infoPublish = Convert2Bean.convertBean(infoPublishReqVo, InfoPublish.class);
        LambdaQueryWrapper<InfoPublish> queryWrapper = new LambdaQueryWrapper<>();
        if (ObjectUtil.isNotEmpty(infoPublish.getId())) {
            queryWrapper.eq(InfoPublish::getId, infoPublish.getId());
        }
        queryWrapper.orderByDesc(InfoPublish::getCreateTime);
        IPage<InfoPublish> selectResult = infoPublishMapper.selectPage(new Page<>(current, size, true), queryWrapper);
        List<InfoPublishRespVo> resultList = selectResult.getRecords().stream()
                .map(bean -> {
                    InfoPublishRespVo infoPublishRespVo = Convert2Bean.convertBean(bean, InfoPublishRespVo.class);
                    return infoPublishRespVo;
                })
                .collect(Collectors.toList());
        Page<InfoPublishRespVo> page = new Page<>(current, size, true);
        page.setCurrent(selectResult.getCurrent());
        page.setTotal(selectResult.getTotal());
        page.setRecords(resultList);
        return page;

    }

    @Override
    public InfoPublishRespVo queryById(String id) {
        InfoPublish infoPublish = baseMapper.selectById(id);
        return Convert2Bean.convertBean(infoPublish, InfoPublishRespVo.class);
    }

    @Override
    public Boolean addInfoPublish(InfoPublishReqVo infoPublishReqVo) {
        infoPublishReqVo.setId("ZX" + IdUtil.simpleUUID());
        InfoPublish infoPublish= Convert2Bean.convertBean(infoPublishReqVo, InfoPublish.class);
        return baseMapper.insert(infoPublish) > 0;
    }

    @Override
    public Boolean updateInfoPublish(InfoPublishReqVo infoPublishReqVo) {
        InfoPublish infoPublish = Convert2Bean.convertBean(infoPublishReqVo, InfoPublish.class);
        LambdaUpdateChainWrapper<InfoPublish> wrapper = new LambdaUpdateChainWrapper<>(infoPublishMapper);
        if (ObjectUtil.isNotEmpty(infoPublish.getCreateUser())) {
            wrapper.set(InfoPublish::getCreateUser, infoPublish.getCreateUser());
        }
        wrapper.eq(InfoPublish::getId, infoPublish.getId());
        return wrapper.update();
    }

    @Override
    public Boolean deleteInfoPublish(String id) {
        return infoPublishMapper.deleteById(id) > 0;
    }
}
